package cumeo89.chii.client.entry.editor;

import static cumeo89.chii.client.entry.Constants.*;

import com.google.gwt.xml.client.Attr;
import com.google.gwt.xml.client.Node;

import cumeo89.chii.client.entry.Editor;
import cumeo89.chii.client.entry.EditorFactory;
import cumeo89.chii.client.util.XMLUtil;

public class DefaultEditorFactory implements EditorFactory {

	private final WikitextEditor WIKITEXT = new WikitextEditor();
	private final CategoryEditor CATEGORY = new CategoryEditor();
	private final FixedSuggestEditor INTERWIKI = new FixedSuggestEditor(
			getInterwikiPrefixes(), getInterwikiNames());
	private final FixedSuggestEditor LANG = new FixedSuggestEditor(
			getLanguageCodes(), getLanguageNames());
	private final FixedSuggestEditor POS = new FixedSuggestEditor(
			getPosCodes(), getPosNames());

	public Editor getEditor(Node node) {
		switch (node.getNodeType()) {
		case Node.ATTRIBUTE_NODE:
			String name = ((Attr) node).getName();
			String tag = XMLUtil.getElement((Attr) node).getTagName();
			if ("content".equals(name) || A_EXAMPLE_EXPLAIN.equals(name)
					|| A_POS_SPELL.equals(name)) {
				return WIKITEXT;
			}
			if (E_CONTEXT.equals(tag) && A_CONTEXT_TEXT.equals(name)) {
				return WIKITEXT;
			}
			if (A_CATEGORY_NAME.equals(name)) {
				return CATEGORY;
			}
			if (E_INTERWIKI.equals(tag) && A_INTERWIKI_LANG.equals(name)) {
				return INTERWIKI;
			}
			if ((A_LANG_CODE.equals(name) && E_LANG.equals(tag))
					|| A_LANG.equals(name)) {
				return LANG;
			}
			if ("code".equals(name) && E_POS.equals(tag)) {
				return POS;
			}
			break;
		case Node.TEXT_NODE:
			return WIKITEXT;
		}
		return null;
	}

}
